cmake_minimum_required(VERSION 3.20)

# 使用vcpkg管理openssl，搜索环境变量VCKPG_ROOT中的vcpkg.cmake。
# note: 该变量必须在project之前设置。工具链文件（Toolchain File）只在 project() 第一次被调用时生效一次。
#    一旦 project() 执行完毕，CMake已根据当时的 变量、路径、编译器、搜索目录生成了内部状态，后面再改 CMAKE_TOOLCHAIN_FILE 也不会重新扫描。
# set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
# message(STATUS "CMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}")

project(jlftp LANGUAGES CXX C)

# 要求编译器支持 C++17（会选用编译器对应的开关：/std:c++17、-std=c++17 等）
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) # 强制必须满足
set(CMAKE_CXX_EXTENSIONS OFF) 

# 判断是否需要支持ssl加密
option(JLFTP_ENABLE_OPENSSL "是否支持ssl加密" OFF)
if(JLFTP_ENABLE_OPENSSL)
    set(TCPSERVER_ENABLE_OPENSSL ON)
    add_compile_definitions(JLFTP_ENABLE_OPENSSL)
else()
    set(TCPSERVER_ENABLE_OPENSSL OFF)
endif()

if(MSVC)
    add_compile_options("/utf-8")
    # 防止WinSock2.h 和 WS2tcpip.h 同时包含导致重定义
    #add_compile_definitions("WIN32_LEAN_AND_MEAN") # 防止WinSock2.h 和 WS2tcpip.h 同时包含导致重定义
    # 默认编译32位
    if(NOT CMAKE_GENERATOR_PLATFORM OR CMAKE_GENERATOR_PLATFORM STREQUAL "")
        set(CMAKE_GENERATOR_PLATFORM "Win32")
    endif()
    message(STATUS "CMAKE_GENERATOR_PLATFORM: ${CMAKE_GENERATOR_PLATFORM}")
else()
    # bug: 32位怎么配置??? 
    # option(BUILD_32BIT "Build 32-bit binaries on 64-bit Linux" OFF)
    # if(BUILD_32BIT)
    #     set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -m32")
    #     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
    #         # 添加链接器 32 位标志
    #     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
    #     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
    #     set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32")
    # endif()
endif()

#find_package(OpenSSL REQUIRED)


if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

# 设置输出路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")

# 添加第三方库
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/3rd/fmt")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/3rd/tcpserver")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/3rd/inifile-cpp")

file(GLOB SRC_FILES
    "${CMAKE_CURRENT_SOURCE_DIR}/src/*.*"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/user/*.*"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/util/*.*"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/memory_pool/*.*"
)

foreach(FILE IN LISTS SRC_FILES)
    message(STATUS "add ${FILE} to project ${CMAKE_PROJECT_NAME}")
endforeach()


add_executable(${PROJECT_NAME} ${SRC_FILES})

target_include_directories(${PROJECT_NAME} PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src/memory_pool
    ${CMAKE_CURRENT_SOURCE_DIR}/src/user
    ${CMAKE_CURRENT_SOURCE_DIR}/src/util
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${TcpServer_SOURCE_DIR}
    ${inifile-cpp_SOURCE_DIR}/include
)

if(ENABLE_OPENSSL)
    target_include_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_INCLUDE_DIR})
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE
    TcpServer
    fmt::fmt
)

if(WIN32)
    target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)
else()
    target_link_libraries(${PROJECT_NAME} PRIVATE pthread)
endif()


# 复制资源文件
# cmake时复制资源
#file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/resource"
#    DESTINATION "${CMAKE_BINARY_DIR}")

# build ${PROJECT_NAMEE}前复制资源(如果源码没有修改，则不会重新build，也不会复制资源)
#add_custom_command(
#    TARGET ${PROJECT_NAME} POST_BUILD
#    COMMAND ${CMAKE_COMMAND} -E copy_directory
#            "${CMAKE_CURRENT_SOURCE_DIR}/resource"
#            "${CMAKE_CURRENT_BINARY_DIR}/resource"
#    COMMENT "Copying resource directory..."
#)

# 添加复制指令。该指令的意义: cmake -E <command> <args>，就能直接执行一些常见操作（复制、删除、创建目录、计算哈希、查看环境变量等）
if(WIN32)
add_custom_target(copy_resource # ALL，好像ALL不加也行？？？
    COMMAND ${CMAKE_COMMAND} -E copy_directory # CMAKE_COMMAND，CMake 可执行文件本身的绝对路径
            "${CMAKE_CURRENT_SOURCE_DIR}/resource"
            "${CMAKE_CURRENT_BINARY_DIR}/resource"
    COMMENT "Copying resource directory..."
    VERBATIM
)
else()
add_custom_target(copy_resource # ALL，好像ALL不加也行？？？
    COMMAND ${CMAKE_COMMAND} -E copy_directory # CMAKE_COMMAND，CMake 可执行文件本身的绝对路径
            "${CMAKE_CURRENT_SOURCE_DIR}/resource"
            "${CMAKE_CURRENT_BINARY_DIR}/bin/resource"
    COMMENT "Copying resource directory..."
    VERBATIM
)

endif()

# 主程序依赖资源，确保资源在主程序链接前复制
add_dependencies(${PROJECT_NAME} copy_resource)